package com.aidl.demo.ui.adapter;


import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aidl.demo.R;
import com.aidl.demo.ui.activity.LiveActivity;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lingchen on 2018/1/27. 15:08
 * mail:lingchen52@foxmail.com
 */
public class LiveListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<LiveActivity.MyLiveData> mLiveListDatas;
    private LayoutInflater mInflater;
    private int mlastPosition;

    public LiveListAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLiveListDatas = new ArrayList<>();
    }

    public void setData(List<LiveActivity.MyLiveData> liveListDatas, int lastPosition) {
        if (liveListDatas.size() >= lastPosition && lastPosition != -1) {
            mlastPosition = lastPosition;
        } else {
            mlastPosition = 0;
        }
        this.mLiveListDatas.clear();
        if (liveListDatas != null && liveListDatas.size() > 0) {
            this.mLiveListDatas.addAll(liveListDatas);
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_recycler_live_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setIsRecyclable(false);
        if (mLiveListDatas.size() > 0) {
            if (position >= 0) {
                if (position == mlastPosition) {
                    viewHolder.mReLiveChannel.requestFocus();
                }
                viewHolder.mTvItemRecyclerLiveNumber.setText(mLiveListDatas.get(position).getNumber());
                viewHolder.mTvItemLiveList.setText(mLiveListDatas.get(position).getName());
                viewHolder.mReLiveChannel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = viewHolder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(viewHolder.mTvItemLiveList, position);
                        mlastPosition = position;
                    }
                });
                viewHolder.mReLiveChannel.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (position == 0 && keyCode == KeyEvent.KEYCODE_DPAD_UP &&
                                event.getAction() == KeyEvent.ACTION_DOWN) {
                            mlastPosition = mLiveListDatas.size() - 1;
                            notifyDataSetChanged();
                            mOnItemKeyDownListener.onItemKeyDown(v, mlastPosition, UP);
                            return true;
                        } else if (position == mLiveListDatas.size() - 1 &&
                                keyCode == KeyEvent.KEYCODE_DPAD_DOWN &&
                                event.getAction() == KeyEvent.ACTION_DOWN) {
                            mlastPosition = 0;
                            notifyDataSetChanged();
                            mOnItemKeyDownListener.onItemKeyDown(v, mlastPosition, DOWN);
                            return true;
                        }
                        return false;
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        return mLiveListDatas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.re_live_channel)
        AutoLinearLayout mReLiveChannel;
        @BindView(R.id.tv_item_recycler_live_number)
        AppCompatTextView mTvItemRecyclerLiveNumber;
        @BindView(R.id.tv_item_recycler_live_list)
        AppCompatTextView mTvItemLiveList;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

    private OnItemClickListener mOnItemClickListener;//声明接口

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public static final int UP = 0;
    public static final int DOWN = 1;

    private OnItemKeyDownListener mOnItemKeyDownListener;//声明接口

    public interface OnItemKeyDownListener {
        void onItemKeyDown(View view, int position, int oritation);
    }

    public void setOnItemKeyDownListener(OnItemKeyDownListener onItemKeyDownListener) {
        mOnItemKeyDownListener = onItemKeyDownListener;
    }
}
